package com.sc.VenusQueryBeta.ui.fragments.home.child.tabs;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.zhouwei.library.CustomPopWindow;
import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.fragment.BaseRecycleFragment;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.api.Host;
import com.sc.VenusQueryBeta.contract.home.tabs.YuDingContract;
import com.sc.VenusQueryBeta.model.bean.home.OrderServiceBean;
import com.sc.VenusQueryBeta.presenter.home.tabs.YuDingPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by SC-02 on 2018/9/19.
 * 预定服务fragment
 */

public class YuDingFragment extends BaseRecycleFragment<YuDingContract.YuDingPresenter> implements YuDingContract.IYuDingView, OnItemClickListener {
    @BindView(R.id.iv_orderservice)
    ImageView ivOrderservice;
    Unbinder unbinder;
    @BindView(R.id.iv_libin)
    ImageView ivLibin;
    private CustomPopWindow mCustomPopWindow;
    @BindView(R.id.ll_book_car)
    LinearLayout yudingCar;
    @BindView(R.id.ll_call_online)
    LinearLayout yudingPhone;
    @BindView(R.id.ll_complain_praise)
    LinearLayout yudingComplain;
    @BindView(R.id.ll_pup_action)
    LinearLayout popupwindowAction;
    private AlertView mCallView;
    private String toPhone;
    private OrderServiceBean morderServiceBean;
    private ImageView ivqrcode, ivqrbig;
    private TextView callphone, wechat;
    private LinearLayout llcallphone, llwechat, llqrcode;


    public static YuDingFragment newInstance() {
        return new YuDingFragment();
    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return YuDingPresenter.newInstance();
    }

    @Override
    public void updateContentList(List<Object> list) {

    }

    @Override
    public void itemNotifyChanged(int position) {

    }

    @Override
    public void showOrderServiceDataSuccess(OrderServiceBean orderServiceBean) {
        morderServiceBean = orderServiceBean;
        if (orderServiceBean.getData() != null) {
            Glide.with(mContext).load(Host.HostImg + orderServiceBean.getData().getConciergeImageUrl()).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.drawable.load_error).into(ivOrderservice);
        }
        hideProgressDialog();
    }

    @Override
    public void showNetworkError() {
        hideProgressDialog();
    }

    @Override
    public void showLoadMoreError() {
        hideProgressDialog();

    }

    @Override
    public void showNoMoreData() {
        hideProgressDialog();

    }

    @Override
    protected void onErrorViewClick(View view) {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_yuding;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
//
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        showProgressDialog("正在加载");
        mPresenter.getOrderServiceData();
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick({R.id.ll_pup_action, R.id.ll_book_car, R.id.ll_call_online, R.id.ll_complain_praise})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_pup_action://弹出小窗口 <!--礼宾部-->
                if (morderServiceBean != null && morderServiceBean.getData() != null) {
                    openPopupWindow(morderServiceBean);
                    ivLibin.setPivotX(ivLibin.getWidth() / 2);
                    ivLibin.setPivotY(ivLibin.getHeight() / 2);//支点在图片中心
                    ivLibin.setRotation(90);
                } else {
                    ToastUtils.showToast(getString(R.string.wujilu));
                }

                break;
            case R.id.ll_book_car://<!--貴賓車預訂-->
                if (morderServiceBean != null && morderServiceBean.getData() != null) {
                    call(morderServiceBean.getData().getVipCarTel());
                } else {
                    ToastUtils.showToast(getString(R.string.wujilu));
                }
                break;
            case R.id.ll_call_online://24小時熱線-->
                if (morderServiceBean != null && morderServiceBean.getData() != null) {
                    call(morderServiceBean.getData().getAllDayTel());
                } else {
                    ToastUtils.showToast(getString(R.string.wujilu));
                }
                break;
            case R.id.ll_complain_praise://<!--表揚及投訴熱線-->
                if (morderServiceBean != null && morderServiceBean.getData() != null) {
                    call(morderServiceBean.getData().getComplaintTel());
                } else {
                    ToastUtils.showToast(getString(R.string.wujilu));
                }
                break;
        }
    }

    /**
     * 点击弹出小窗口
     *
     * @param morderServiceBean
     */

    private void openPopupWindow(final OrderServiceBean morderServiceBean) {
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow, null);
        //处理popWindow 显示内容
        callphone = contentView.findViewById(R.id.call_phone);
        callphone.setText(morderServiceBean.getData().getVipCarTel() == null ? "" : morderServiceBean.getData().getVipCarTel());

        wechat = contentView.findViewById(R.id.wechat);
        wechat.setText(morderServiceBean.getData().getWechatId() == null ? "" : morderServiceBean.getData().getWechatId());

        ivqrcode = contentView.findViewById(R.id.iv_qr_code);
        Glide.with(mContext).load(Host.HostImg + morderServiceBean.getData().getQrCodeUrl()).diskCacheStrategy(DiskCacheStrategy.SOURCE).centerCrop().placeholder(R.drawable.load_error).into(ivqrcode);

        llcallphone = contentView.findViewById(R.id.ll_call_phone);
        llwechat = contentView.findViewById(R.id.ll_wechat);
        llqrcode = contentView.findViewById(R.id.ll_qrcode);


        //打電話
        llcallphone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (morderServiceBean != null && morderServiceBean.getData() != null) {
                    call(morderServiceBean.getData().getVipCarTel());
                    mCustomPopWindow.dissmiss();
                }
            }
        });


        //複製微信名
        llwechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager)
                        mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", morderServiceBean.getData().getWechatId());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtils.showToast("微信 ID 成功複製");
                mCustomPopWindow.dissmiss();
            }

        });

        //查看二維碼
        llqrcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCustomPopWindow.dissmiss();
                openqrcodeWindow(morderServiceBean);
            }
        });

        //创建并显示popWindow
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(getActivity())
                    .setView(contentView)
                    .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                    .setBgDarkAlpha(0.6f) // 控制亮度
                    .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            ivLibin.setPivotX(ivLibin.getWidth() / 2);
                            ivLibin.setPivotY(ivLibin.getHeight() / 2);//支点在图片中心
                            ivLibin.setRotation(360);
                        }
                    })
                    .create()
                    .showAsDropDown(popupwindowAction, 0, -80, Gravity.RIGHT);
        }


    }

    /**
     * 打电话
     *
     * @param phone 号码
     */
    private void call(String phone) {
        toPhone = phone;
        mCallView = new AlertView("拨打电话", phone, "取消", new String[]{"呼叫"}, null, mActivity, AlertView.Style.Alert, this).setCancelable(true);
        mCallView.show();
        //监听消失
        mCallView.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(Object o) {
//                closeKeyboard();
            }
        });
    }

    @Override
    public void onItemClick(Object o, int position) {
//        closeKeyboard();
        if (position != AlertView.CANCELPOSITION) {//呼叫按钮
            //用intent启动拨打电话
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + toPhone));
            startActivity(intent);
        }
    }

    private void openqrcodeWindow(final OrderServiceBean morderServiceBean) {
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_qrcode, null);
        //处理popWindow 显示内容
        ivqrbig = contentView.findViewById(R.id.iv_bigqr);
        Glide.with(mContext).load(Host.HostImg + morderServiceBean.getData().getQrCodeUrl()).diskCacheStrategy(DiskCacheStrategy.SOURCE).centerCrop().placeholder(R.drawable.load_error).into(ivqrbig);
        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(getActivity())
                .setView(contentView)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.1f) // 控制亮度
                .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                    }
                })
                .create()
                .showAtLocation(popupwindowAction, Gravity.CENTER, 0, 0);
    }

}
